﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using WMSADBUtility;
using WMSAML.Warehouse;

namespace WMSADL.Warehouse
{
    public class PD_PickingListDAL
    {
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int NID)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from PD_PickingList where NID=@NID ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "NID", DbType.Int32, NID);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByPickingNumber(String PickNO)
        {
            Database db = DatabaseFactory.CreateDatabase();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from PD_PickingList where PickNO=@PickNO ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PickNO", DbType.String, PickNO);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

 
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(PD_PickingListEntity entity)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update PD_PickingList set ");
            strSql.Append("PickNO=@PickNO,");
            strSql.Append("ProductNO=@ProductNO,");
            strSql.Append("NeedQty=@NeedQty,");
            strSql.Append("PickQty=@PickQty,");
            strSql.Append("Unit=@Unit,");
            strSql.Append("Line=@Line,");
            strSql.Append("Lot=@Lot,");
            strSql.Append("Model=@Model,");
            strSql.Append("PO=@PO,");
            strSql.Append("Stauts=@Stauts,");
            strSql.Append("CreatTime=@CreatTime,");
            strSql.Append("FinishTime=@FinishTime,");
            strSql.Append("UserName=@UserName,");
            strSql.Append("Mark=@Mark,");
            strSql.Append("field1=@field1,");
            strSql.Append("field2=@field2,");
            strSql.Append("field3=@field3,");
            strSql.Append("field4=@field4,");
            strSql.Append("field5=@field5");
            strSql.Append(" where NID=@NID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "NID", DbType.Int32, entity.NID);
            db.AddInParameter(dbCommand, "PickNO", DbType.String, entity.PickNO);
            db.AddInParameter(dbCommand, "ProductNO", DbType.String, entity.ProductNO);
            db.AddInParameter(dbCommand, "NeedQty", DbType.Decimal, entity.NeedQty);
            db.AddInParameter(dbCommand, "PickQty", DbType.Decimal, entity.PickQty);
            db.AddInParameter(dbCommand, "Unit", DbType.String, entity.Unit);
            db.AddInParameter(dbCommand, "Line", DbType.String, entity.Line);
            db.AddInParameter(dbCommand, "Lot", DbType.String, entity.Lot);
            db.AddInParameter(dbCommand, "Model", DbType.String, entity.Model);
            db.AddInParameter(dbCommand, "PO", DbType.String, entity.PO);
            db.AddInParameter(dbCommand, "Stauts", DbType.String, entity.Stauts);
            db.AddInParameter(dbCommand, "CreatTime", DbType.DateTime, entity.CreatTime);
            db.AddInParameter(dbCommand, "FinishTime", DbType.DateTime, entity.FinishTime);
            db.AddInParameter(dbCommand, "UserName", DbType.String, entity.UserName);
            db.AddInParameter(dbCommand, "Mark", DbType.String, entity.Mark);
            db.AddInParameter(dbCommand, "field1", DbType.String, entity.field1);
            db.AddInParameter(dbCommand, "field2", DbType.String, entity.field2);
            db.AddInParameter(dbCommand, "field3", DbType.String, entity.field3);
            db.AddInParameter(dbCommand, "field4", DbType.String, entity.field4);
            db.AddInParameter(dbCommand, "field5", DbType.String, entity.field5);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string PickNO)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from PD_PickingList ");
            strSql.Append(" where PickNO=@PickNO ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "PickNO", DbType.String, PickNO);
            int rows = db.ExecuteNonQuery(dbCommand);

            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        } 

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PD_PickingListEntity GetEntity(int NID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select NID,PickNO,ProductNO,NeedQty,PickQty,Unit,Line,Lot,Model,PO,Stauts,CreatTime,FinishTime,UserName,Mark,field1,field2,field3,field4,field5 from PD_PickingList  ");
            strSql.Append(" where NID=@NID ");
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "NID", DbType.Int32, NID);
            PD_PickingListEntity model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
            }
            return model;
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public PD_PickingListEntity DataRowToEntity(DataRow row)
        {
            PD_PickingListEntity entity = new PD_PickingListEntity();
            if (row != null)
            {
                if (row["NID"] != null && row["NID"].ToString() != "")
                {
                    entity.NID = int.Parse(row["NID"].ToString());
                }
                if (row["PickNO"] != null)
                {
                    entity.PickNO = row["PickNO"].ToString();
                }
                if (row["ProductNO"] != null)
                {
                    entity.ProductNO = row["ProductNO"].ToString();
                }
                if (row["NeedQty"] != null && row["NeedQty"].ToString() != "")
                {
                    entity.NeedQty = decimal.Parse(row["NeedQty"].ToString());
                }
                if (row["PickQty"] != null && row["PickQty"].ToString() != "")
                {
                    entity.PickQty = decimal.Parse(row["PickQty"].ToString());
                }
                if (row["Unit"] != null)
                {
                    entity.Unit = row["Unit"].ToString();
                }
                if (row["Line"] != null)
                {
                    entity.Line = row["Line"].ToString();
                }
                if (row["Lot"] != null)
                {
                    entity.Lot = row["Lot"].ToString();
                }
                if (row["Model"] != null)
                {
                    entity.Model = row["Model"].ToString();
                }
                if (row["PO"] != null)
                {
                    entity.PO = row["PO"].ToString();
                }
                if (row["Stauts"] != null)
                {
                    entity.Stauts = row["Stauts"].ToString();
                }
                if (row["CreatTime"] != null && row["CreatTime"].ToString() != "")
                {
                    entity.CreatTime = DateTime.Parse(row["CreatTime"].ToString());
                }
                if (row["FinishTime"] != null && row["FinishTime"].ToString() != "")
                {
                    entity.FinishTime = DateTime.Parse(row["FinishTime"].ToString());
                }
                if (row["UserName"] != null)
                {
                    entity.UserName = row["UserName"].ToString();
                }
                if (row["Mark"] != null)
                {
                    entity.Mark = row["Mark"].ToString();
                }
                if (row["field1"] != null)
                {
                    entity.field1 = row["field1"].ToString();
                }
                if (row["field2"] != null)
                {
                    entity.field2 = row["field2"].ToString();
                }
                if (row["field3"] != null)
                {
                    entity.field3 = row["field3"].ToString();
                }
                if (row["field4"] != null)
                {
                    entity.field4 = row["field4"].ToString();
                }
                if (row["field5"] != null)
                {
                    entity.field5 = row["field5"].ToString();
                }
            }
            return entity;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,PickNO,ProductNO,NeedQty,PickQty,Unit,Line,Lot,Model,PO,Stauts,CreatTime,FinishTime,UserName,Mark,field1,field2,field3,field4,field5  ");
            strSql.Append(" FROM PD_PickingList ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase();
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }
        

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public List<PD_PickingListEntity> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select NID,PickNO,ProductNO,NeedQty,PickQty,Unit,Line,Lot,Model,PO,Stauts,CreatTime,FinishTime,UserName,Mark,field1,field2,field3,field4,field5 ");
            strSql.Append(" FROM PD_PickingList ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<PD_PickingListEntity> list = new List<PD_PickingListEntity>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
            }
            return list;
        }
         
        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public PD_PickingListEntity ReaderBind(IDataReader dataReader)
        {
            PD_PickingListEntity entity = new PD_PickingListEntity();
            object ojb;
            ojb = dataReader["NID"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.NID = (int)ojb;
            }
            entity.PickNO = dataReader["PickNO"].ToString();
            entity.ProductNO = dataReader["ProductNO"].ToString();
            ojb = dataReader["NeedQty"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.NeedQty = (decimal)ojb;
            }
            ojb = dataReader["PickQty"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.PickQty = (decimal)ojb;
            }
            entity.Unit = dataReader["Unit"].ToString();
            entity.Line = dataReader["Line"].ToString();
            entity.Lot = dataReader["Lot"].ToString();
            entity.Model = dataReader["Model"].ToString();
            entity.PO = dataReader["PO"].ToString();
            entity.Stauts = dataReader["Stauts"].ToString();
            ojb = dataReader["CreatTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.CreatTime = (DateTime)ojb;
            }
            ojb = dataReader["FinishTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                entity.FinishTime = (DateTime)ojb;
            }
            entity.UserName = dataReader["UserName"].ToString();
            entity.Mark = dataReader["Mark"].ToString();
            entity.field1 = dataReader["field1"].ToString();
            entity.field2 = dataReader["field2"].ToString();
            entity.field3 = dataReader["field3"].ToString();
            entity.field4 = dataReader["field4"].ToString();
            entity.field5 = dataReader["field5"].ToString();
            return entity;
        }

        #endregion  Method
    }
}
